#define SCI_NAMESPACE 1

#include <AppKit/AppKit.h>
#include <WebKit/WebView.h>
#include <WebKit/WebFrame.h>a
#include <WebKit/WebPolicyDelegate.h>
#include <WebKit/WebFrameLoadDelegate.h>
#include <WebKit/WebDataSource.h>
#include <ApplicationServices/ApplicationServices.h>

#include <brl.mod/blitz.mod/blitz.h>
#include <maxgui.mod/maxgui.mod/maxgui.h>
#include <pub.mod/macos.mod/macos.h>

#import "include/ScintillaView.h"



using namespace Scintilla;

@interface BBScintillaView : ScintillaView{}
-(NSMenu *)menuForEvent:(NSEvent*)theEvent;
@end

static void SCN_notification(intptr_t windowid, unsigned int iMessage, uintptr_t wParam, uintptr_t lParam);

int SCNSendMessage(ScintillaView *obj, int message, int wParam, int lParam){return ScintillaCocoa::DirectFunction(obj->mBackend, message, wParam, lParam);}

extern "C"
{
	int klepto_scintilla_PostScintillaGuiEvent(int ev,void *handle,int data,int mods,int x,int y,BBObject *extra );
	BBScintillaView *GenerateScintillaView(NSView *view);	
	void RegisterNotification(BBScintillaView *obj, SciNotifyFunc func);
	void SendEditorW(BBScintillaView *view, unsigned int iMessage, uptr_t wParam, sptr_t lParam);
}


static void PostGuiEvent( int ev,void *handle,int data,int mods,int x,int y,BBObject *extra ){
	if (extra==0) extra=&bbNullObject;
	klepto_scintilla_PostScintillaGuiEvent(ev,handle,data,mods,x,y,extra);
}

BBScintillaView *GenerateScintillaView(NSView *view)
{
  NSRect newFrame = view.frame;
  newFrame.size.width -= 2 * newFrame.origin.x;
  newFrame.size.height -= 3 * newFrame.origin.y;
  
  BBScintillaView *mEditor = [[[BBScintillaView alloc] initWithFrame: newFrame] autorelease];
  
  [view addSubview: mEditor];
  [view setAutoresizesSubviews: YES];
  [mEditor setAutoresizesSubviews: YES];
  [mEditor setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];

  return mEditor;
}

void RegisterNotification(BBScintillaView *obj, SciNotifyFunc func)
{
	obj->mBackend->RegisterNotifyCallback((intptr_t)obj, func);
}


void SendEditorW(BBScintillaView *view, unsigned int iMessage, uptr_t wParam, sptr_t lParam)
{
	printf("Test : %i",iMessage);
	ScintillaCocoa::DirectFunction(((ScintillaCocoa *)view), iMessage, wParam,  lParam);
	//((ScintillaCocoa *)view)->WndProc(iMessage,wParam,lParam);
}

@implementation BBScintillaView
-(NSMenu *)menuForEvent:(NSEvent *)event{
	NSPoint	p;
	int		x,y;
	p=[event locationInWindow];	
	x=(int)p.x;y=(int)p.y;
	PostGuiEvent( BBEVENT_GADGETMENU,self,0,0,x,y,0 );
	return nil;
}
@end










